package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TGoodsEntity;
import com.gmail.blackdog1987.onedollar.entity.TGoodsJudgeEntity;
import com.gmail.blackdog1987.onedollar.entity.TGoodsShowEntity;
import com.gmail.blackdog1987.onedollar.entity.TOrderRefundEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.GoodsJudgeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("goodsJudgeDao")
public class GoodsJudgeDao extends EntityHibernateDao<TGoodsJudgeEntity>{
    @Autowired
    BaseJdbcDao baseJdbcDao;

    public Page<GoodsJudgeVO> queryForPage(int pageindex, int pagesize, int goodsid){
        String whereSql;
        whereSql =
                "FROM t_goods_judge\n"+
                "LEFT JOIN t_user ON t_user.user_id = t_goods_judge.user_id\n" +
                "WHERE t_goods_judge.goods_id = ?\n" +
                "ORDER BY t_goods_judge.add_time DESC ";

        String selectionSql =
                "SELECT t_goods_judge.*,\n" +
                "       t_user.nick_name,\n" +
                "       t_user.head\n" ;


        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + whereSql , new Object[]{goodsid});

        List<GoodsJudgeVO> list = JdbcUtil.queryForList(baseJdbcDao, GoodsJudgeVO.class, selectionSql + whereSql + limit, new Object[]{goodsid});

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }
}
